package com.yindong.tabbar.fraction;

import com.yindong.tabbar.ResourceTable;
import com.yindong.tabbar.model.TabBarInfoModel;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;
import ohos.app.Context;

import java.util.List;

/**
 * fraction页面组件
 *      使用StackLayout布局，与tabbar统一
 *
 * @author      殷冬
 * @date        2021-01-19
 * @version     1.0.0
 */
public class FractionPageComponent extends StackLayout {

    /**
     * Fraction适配器
     */
    private FractionAdapter adapter;

    /**
     * 当前位置
     */
    private Integer currentIndex;

    /**
     * Fraction组件对象
     */
    public static FractionPageComponent fractionPageComponent;

    public FractionPageComponent(Context context) {
        super(context);
        // 使用abilitySlice.findComponentById获取的页面id为空，改为LayoutScatter.getInstance(context).parse 方式获取
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_bottom_navigation, this, true);
        fractionPageComponent = (FractionPageComponent) component.findComponentById(ResourceTable.Id_fraction_page_component);
    }

    public FractionPageComponent(Context context, AttrSet attrSet) {
        super(context, attrSet);
    }

    public FractionPageComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    /**
     * 设置适配器
     *
     * @param adapter       适配器对象
     */
    public void setAdapter(FractionAdapter adapter){
        this.adapter = adapter;
        this.currentIndex = 0;
    }

    /**
     * 根据点击tabbar坐标，显示Fraction
     *
     * @param nextIndex                 点击tabbar返回的坐标
     * @param nextTabBarInfoModel       点击的tabbar配置信息
     * @param currentTabBarInfoModel    当前的tabbar配置信息
     */
    public void showCurrentFractionByIndex(Integer nextIndex, TabBarInfoModel nextTabBarInfoModel, TabBarInfoModel currentTabBarInfoModel){
        if(this.currentIndex != nextIndex){
            // 隐藏当前的tabbar对应的Fraction页面
            adapter.hideFraction(this, currentIndex, currentTabBarInfoModel);
            // 显示点击的tabbar对应的Fraction页面
            adapter.showFraction(this, nextIndex, nextTabBarInfoModel);
            this.currentIndex = nextIndex;
        }
    }

    /**
     * 初始化所有Fraction页面
     *      如果只初始化第一次进入选中页面，会导致其他的页面监听运行抛异常
     *
     * @param tabBarInfoModelLists
     */
    public void initAllFractionPage(List<TabBarInfoModel> tabBarInfoModelLists){
        // 取出当前的
        TabBarInfoModel currentTabBarInfoModel = tabBarInfoModelLists.get(currentIndex);
//        tabBarInfoModelLists.remove(currentIndex);
        for(Integer i = 0; i < tabBarInfoModelLists.size(); i++){
            // 不初始化当前的Fraction
            if(i != currentIndex){
                // 初始化Fraction
                adapter.initFractionSchedule(this, i, tabBarInfoModelLists.get(i));
                // 隐藏Fraction
                adapter.hideFraction(this, i, tabBarInfoModelLists.get(i));
            }
        }
        // 最后初始化默认选中的Fraction页面， Fraction管理器默认最后一个添加的Fraction显示， 用次种形式与tabbar选中匹配
        adapter.initFractionSchedule(this, currentIndex, currentTabBarInfoModel);
    }

}
